package com.gopay.scheduler.job;

import com.gopay.common.constants.fund.FundAcctStrEnum;
import com.gopay.common.cps.service.fund.FundOrderService;
import com.gopay.common.domain.acps.model.AcctMastBean;
import com.gopay.common.domain.cps.CpsGenMainOrder;
import com.gopay.common.domain.cps.transfer.Tran02020Detail;
import com.gopay.common.domain.cps.transfer.Tran02020VO;
import com.gopay.common.domain.fund.FundGopayCpsGenOrder;
import com.gopay.common.util.DateUtils;
import com.gopay.cron.service.fund.FundFastPayService;
import com.gopay.remote.fundcps.service.FundCpsGenOrderService;
import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.service.fund.FundAcctMastService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 活期宝定时生成02018付款单
 * @author dong-wang5@hnair.com
 * @date 2017-12-26 11:00:49
 */
@Controller
public class FundpFastPayJob extends GopayBaseJob {

    /** logger */
    private static final Logger logger = LoggerFactory.getLogger(FundpFastPayJob.class);

    @Resource(name = "fundCpsGenOrderService")
    private FundCpsGenOrderService fundCpsGenOrderService;

    @Autowired
    private FundOrderService fundOrderService;

    @Autowired
    private FundAcctMastService fundAcctMastService;

    @Autowired
    private FundFastPayService fundFastPayService;

    @Override
    protected void doJobInternal() {
        logger.info("【活期宝快速赎回付款处理】生成活期宝02018订单，开始！=============" + DateUtils.format(new Date()), DateUtils.yyyy_MM_dd_HH_mm_ss);
        String settleDay = DateUtils.format(new Date(), DateUtils.FILEDATE_PATTERM);
        Date startTime = DateUtils.add(new Date(), Calendar.DATE, -7);
        Date endTime = DateUtils.getSystemDate();
        fundFastPayService.fastPay(startTime,endTime);
        logger.info("【活期宝快速赎回付款处理】执行完毕！");
    }

    /**
     * 商财通，退款传输订单初始化
     * @author yhwpeng@126.com
     * @date 2017-12-28 16:15:36
     * @params
     * @param mainOrders02020 : 活期宝订单明细
     * @param gopayOrderId02018 : 02018订单号
     * @return com.gopay.common.domain.cps.transfer.Tran02014VO
     */
    public Tran02020VO init02020TranVo(List<CpsGenMainOrder> mainOrders02020, String gopayOrderId02018, String userId) {
        List<Tran02020Detail> voList = new ArrayList<>();
        for (CpsGenMainOrder order : mainOrders02020){
            Tran02020Detail vo  = new Tran02020Detail();
            vo.setGopayOrderId(order.getGopayOrderId());
            vo.setUserId(userId);
            vo.setMerTxnAmt(order.getTotalOrderAmt());
            voList.add(vo);
        }
        Tran02020VO tran02020VO = new Tran02020VO();
        tran02020VO.setVoList(voList);
        tran02020VO.setMainGopayOrderId(gopayOrderId02018);
        FundGopayCpsGenOrder fundGopayOrder02018 = fundOrderService.getFundOrderByGopayOrderId(gopayOrderId02018);
        AcctMastBean account = fundAcctMastService.getAccount(fundGopayOrder02018.getMerchantId(), FundAcctStrEnum.FDSFT.getValue());
        tran02020VO.setAcctIdSFT(account.getAcctId());
        return tran02020VO;
    }
}
